package logicals

import (
	"easyblog/models"
	"easyblog/utils/db"
	"easyblog/utils/errmsg"
	"easyblog/utils/tools"
	"gorm.io/gorm"
	"strconv"
	"strings"
)

type TagLogical struct {}

var DefaultTag = TagLogical{}

/**
* CreateTag
* @Description: 创建标签
* @receiver this
* @param tag
* @return int
*/
func (this *TagLogical) CreateTag(tag *models.Tag) int {
	err := db.Db.Create(tag).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCSE
}

/**
* EditTag
* @Description: 编辑标签
* @receiver this
* @param tagId
* @param tag
* @return int
*/
func (this *TagLogical) EditTag(tagId int, tag *models.Tag) int {
	var maps = make(map[string]interface{})
	maps["name"] = tag.Name

	err := db.Db.Model(&models.Tag{}).Where("id = ?", tagId).Updates(maps).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCSE
}

/**
* GetTagByTid
* @Description: 根据id获得一个标签
* @receiver this
* @param tid
* @return models.Tag
* @return int
*/
func (this *TagLogical) GetTagById(tid int)(models.Tag,int){
	var tag models.Tag
	err := db.Db.Where("id = ?", tid).First(&tag).Error
	if err != nil {
		return tag,errmsg.ERROR
	}
	return tag,errmsg.SUCCSE
}

/**
* GetTagsByTids
* @Description: 根据多个id，获得多个标签
* @receiver this
* @param tidsString 1,2,3
* @return []models.Tag
* @return int
*/
func (this *TagLogical) GetTagsByIds(tidsString string)([]models.Tag,int){
	var tagList []models.Tag
	tidStrings := strings.Split(tidsString, ",")
	for _,tidString := range tidStrings {
		tid,_ := strconv.Atoi(tidString)
		tagTemp,code := this.GetTagById(tid)
		if code == errmsg.ERROR{
			return nil,errmsg.ERROR
		}
		tagList = append(tagList, tagTemp)
	}
	return tagList,errmsg.SUCCSE
}

/**
* GetPageTags(pageSize int, pageNum int)
* @Description:  分页查询标签
* @receiver this
* @param pageSize
* @param pageNum
* @return []models.Tag
* @return int64
*/
func (this *TagLogical)  GetPageTags(pageSize int, pageNum int) ([]models.Tag, int64){
	var tags []models.Tag
	var total int64

	err := db.Db.Find(&tags).Limit(pageSize).Offset(pageSize * (pageNum - 1)).Error
	db.Db.Model(&tags).Count(&total)
	// 不存在
	if err != nil && gorm.ErrRecordNotFound != nil {
		return nil, 0
	}
	return tags, total
}


/**
* DeleteCategory
* @Description: 删除标签
* @receiver this
* @param categoryId
* @return int
*/
func (this *TagLogical) DeleteTag(tagId int) int {
	err := db.Db.Where("id = ?", tagId).Delete(&models.Tag{}).Error
	if err != nil {
		return errmsg.ERROR
	}
	return errmsg.SUCCSE
}

/**
* GetTags
* @Description: 获取前20个tag
* @receiver this
* @return interface{}
* @return interface{}
*/
func (this *TagLogical) GetHotTags() ([]models.Tag, int) {
	var tags []models.Tag
	err := db.Db.Find(&tags).Limit(20).Error

	if err != nil {
		return nil, errmsg.ERROR
	}
	return tags, errmsg.SUCCSE
}

/**
* updateTagsNum
* @Description: 更新tag下文章的数量
* @param tagIds
* @param action
*/
func updateTagsNum(tagIds string , action string){

	tagIdList := tools.String2list(tagIds)

	err := db.Db.Model(&models.Tag{}).Where("id IN (?)", tagIdList).UpdateColumn("num", gorm.Expr("num "+action+" 1")).Error
	if err != nil{
		return
	}
	return
}